package org.learn.controller;

import com.alibaba.fastjson.JSONObject;
import org.learn.jwt.JwtUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/user")
public class UserController {

    /**
     * 用戶控制器
     *
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public JSONObject submitLogin(String userName, String password, HttpServletRequest req, HttpServletResponse rep, Model model) throws Exception {
        JSONObject jsonObject = new JSONObject();
        if (userName.equals("test1") && "123456".equals(password)) {
            String token = JwtUtil.createToken("userInfo", userName);//创建token
            jsonObject.put("token", token);
        } else {
            jsonObject.put("msg", "用户名或密码错误!");
        }

        return jsonObject;
    }


}
